package com.example.iwalk.newprintapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.example.iwalk.newprintapplication.R;
import com.example.iwalk.newprintapplication.inter.AdapterButtonClickListener;

import java.util.List;

/**
 * Created by iwalk on 2016/12/23.
 */

public class IpAddressAdapter extends BaseAdapter {
    private Context context;
    private List<String> list;
    private AdapterButtonClickListener listener;

    public IpAddressAdapter(List list, Context context) {
        this.list = list;
        this.context = context;
    }

    public void setListener(AdapterButtonClickListener listener) {
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_ip_address, null);
            viewHolder = new ViewHolder();
            viewHolder.tv_ip_address = (TextView) view.findViewById(R.id.tv_ip_address);
            viewHolder.tv_connect_state = (TextView) view.findViewById(R.id.tv_connect_state);
            viewHolder.btn_ip_test = (Button) view.findViewById(R.id.btn_ip_test);
            viewHolder.btn_ip_test.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onButtonClick(i,view);
                }
            });
            view.setTag(viewHolder);
        } else {
            viewHolder = ((ViewHolder) view.getTag());
        }
        viewHolder.tv_ip_address.setText(list.get(i));
        return view;
    }

    class ViewHolder {
        TextView tv_ip_address,tv_connect_state;
        Button btn_ip_test;
    }
}
